<?php
namespace app\index\controller;

use app\common\model\Users;
use think\Db;

class DailiAjax extends Ajax
{
    protected $userInfo;

    public function _empty()
    {
		if(check_mobile()){
			exit($this->fetch('mobile/common/empty'));
		}else{
			exit($this->fetch('pc/common/empty'));
		}
    }


    public function cz($act)
    {
        if (request()->isAjax()) {
            $result = [
                'code' => -1,
                'message' => '无此操作'
            ];
            switch ($act) {
                case 'price':
                    $data = input('post.');
                    switch ($data['type']) {
                        case 0://VIP价格
                            $oprice = config('sys_vip_price_' . $data['id'] . '');
                            $zk = config('sys_dl_give_z_' . $this->userInfo['dlpower'] . '');
                            $price = round($oprice * $zk / 10, 2);
							echo "<script>$(\"#jiage\").html('<font class=\"bold pr0\">开通价格：</font><font class=\"f6 hong\">".$price."</font> 元 <font class=\"f3 hui\">原价<s> ".$oprice." </s>元 ".$zk."折优惠中</font>');</script>";
                            break;
                        case 1://配额价格
                            $oprice = config('sys_pe_price_' . $data['id'] . '');
                            $zk = config('sys_dl_give_z_' . $this->userInfo['dlpower'] . '');
                            $price = round($oprice * $zk / 10, 2);
                            echo "<script>$(\"#jiage\").html('<font class=\"bold pr0\">开通价格：</font><font class=\"f6 hong\">".$price."</font> 元 <font class=\"f3 hui\">原价<s> ".$oprice." </s>元 ".$zk."折优惠中</font>');</script>";
                            break;
                    }
                    break;
                case 'cz'://进行充值
                    $data = input('post.');
					$user = Users::where('uid', '=' ,$data['uid'])->find();
					if(!$user){//不存在则返回
						echo '<script>x.msg(\'充值的用户UID不存在\');</script>';
					}else{//存在则执行
						if($data['type']=='vip'){//判断充值类型
							$oprice = config('sys_vip_price_' . $data['vip_ver'] . '');
                            $zk = config('sys_dl_give_z_' . $this->userInfo['dlpower'] . '');
                            $price = round($oprice * $zk / 10, 2);//开通所需余额
							
							if($this->userInfo['money'] > $price){
								$new_money = $this->userInfo['money'] - $price;
								//开通时间
								$vip_start = date("Y-m-d");
								//计算开通的vip时长
								if ($user['viptime']) {
									$vip_time = date("Y-m-d", strtotime("+" . is_vip_day($data['vip_ver']) . " day", strtotime($user['viptime'])));
								} else {
									$vip_time = date("Y-m-d", strtotime("+" . is_vip_day($data['vip_ver']) . " day"));
								}
								$new=[
									'money' => $new_money
								];
								if(Users::updateByUid($this->userInfo['uid'],$new)){
									$cz_data=[
										'vipstart' => $vip_start,
										'viptime' => $vip_time
									];
									Users::updateByUid($data['uid'],$cz_data);
									echo '<script>x.msg(\'为用户 '.$user['nickname'].' 充值VIP成功\');</script>';
								}else{
									echo '<script>x.msg(\'网络连接错误\');</script>';
								}
							}else{
								echo '<script>x.msg(\'你的账户余额不足\');</script>';
							}
						}else{
							$oprice = config('sys_pe_price_' . $data['peie_ver'] . '');
                            $zk = config('sys_dl_give_z_' . $this->userInfo['dlpower'] . '');
                            $price = round($oprice * $zk / 10, 2);//开通所需余额
							
							if($this->userInfo['money'] > $price){
								$new_money = $this->userInfo['money'] - $price;
								//获取购买配额数量
								$res_peie = is_peie_num($data['peie_ver']);
								//计算用户配额总数
								$all_peie = $user['peie'] + $res_peie;
								//计算用户剩下的余额
								$new_money = $this->userInfo['money'] - $price;
								$new=[
									'money' => $new_money
								];
								if(Users::updateByUid($this->userInfo['uid'],$new)){
									$cz_data=[
										'peie' => $all_peie
									];
									Users::updateByUid($data['uid'],$cz_data);
									echo '<script>x.msg(\'为用户 '.$user['nickname'].' 充值配额成功\');</script>';
								}else{
									echo '<script>x.msg(\'网络连接错误\');</script>';
								}
							}else{
								echo '<script>x.msg(\'你的账户余额不足\');</script>';
							}
						}
					}
                    break;
            }
        }
    }

    protected
    function _initialize()
    {
        parent::_initialize();
        $this->checkLogin();
		
    }
	
	private
    function checkLogin()
    {
        $check_Login = Users::checkLogin();
        if ($check_Login != null) {
            $this->assign('alert', $check_Login['msg']);
            $this->assign('url', $check_Login['url']);
			if(check_mobile()){
				exit($this->fetch('mobile/common/msg'));
			}else{
				exit($this->fetch('pc/common/msg'));
			}
        } else {
            $this->userInfo = Users::getLoginUser();
            $this->assign('userInfo', $this->userInfo);

        }
    }
}